package com.lwl.Utils;

import java.util.Random;

public class LLinkNode {

    public int value;

    public LLinkNode next;

    public LLinkNode pre;

    public LLinkNode() {
    }

    public LLinkNode(int value) {
        this.value = value;
    }

    public LLinkNode(LLinkNode next) {
        this.next = next;
    }

    public LLinkNode(int value, LLinkNode next) {
        this.value = value;
        this.next = next;
    }

    public LLinkNode(int value, LLinkNode next, LLinkNode pre) {
        this.value = value;
        this.next = next;
        this.pre = pre;
    }

    public static void printLinkList(LLinkNode head) {
        while (head != null) {
            System.out.print(head.value + " ");
            head = head.next;
        }
        System.out.println();
    }

    public static void reversePrintLinkList(LLinkNode head) {
        if (head == null) {
            return;
        }

        while (head.next != null) {
            head = head.next;
        }

        while (head != null) {
            System.out.print(head.value + " ");
            head = head.pre;
        }
        System.out.println();
    }

    public void printLinkList() {
        printLinkList(this);
    }

    public void reversePrintLinkList() {
        reversePrintLinkList(this);
    }

    public static LLinkNode createRandomLink() {
        Random random = new Random();
        int len = random.nextInt(20);
        if (len < 0) len = 15;
        LLinkNode head = new LLinkNode(random.nextInt(10));
        LLinkNode t = head;
        for (int i = 0; i < len; i++) {
            t.next = new LLinkNode(random.nextInt(10));
            t = t.next;
        }
        return head;
    }

}
